package com.platform.service;

import com.platform.dao.ApiCallUserBillMapper;
import com.platform.dao.ApiCircleMapper;
import com.platform.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.SQLOutput;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Service接口
 *
 * @author zhurusen
 * @email 524227263@qq.com
 * @date 2018-05-14 23:16:55
 */
@Service
public class ApiCallUserBillService {
    @Autowired
    private ApiCallUserBillMapper apiCallUserBillMapper;
    @Autowired
    private ApiCallUserBillService apiCallUserBillService;
    @Autowired
    private ApiUserService apiUserService;
    @Autowired
    private ApiAmountBillService apiAmountBillService;
    @Autowired
    private ApiOrderService apiOrderService;
    @Autowired
    private ApiCallBillService apiCallBillService;

    public void save(ApiCallUserBillVo apiCallUserBillVo) {
        apiCallUserBillMapper.save(apiCallUserBillVo);
    }

    /**
     * 更新每分钟的通话记录
     *
     * @param callNo
     */
    public void updataCallList(String callNo) {
        apiCallUserBillMapper.updateListByCallNo(callNo);
    }

    public List<ApiCallUserBillVo> selectList(Map<String, Object> params) {
        return apiCallUserBillMapper.queryList(params);
    }

    public void updata(ApiCallUserBillVo info) {
        apiCallUserBillMapper.update(info);
    }

    public void updateCallList(UserVo userVo) {
        System.out.println("******************刷新异常退出通话*******************");
        /**
         * 获取当前人 所有异常退出的记录
         */
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userVo.getId());
        List<ApiCallUserBillVo> userBill = apiCallUserBillMapper.queryListByUserId(map);

        for (ApiCallUserBillVo info : userBill) {
            Map<String, Object> params = new HashMap<>();
            params.put("callNo", info.getCallNo());
            List<ApiCallUserBillVo> userBillVos = apiCallUserBillService.selectList(params);

            /**
             * 通话市场
             */
            Long talkTime = (long) userBillVos.size();
            BigDecimal amount = new BigDecimal(0);

            for (ApiCallUserBillVo infoVo : userBillVos) {
                infoVo.setStatus(2);
                infoVo.setCallNo(infoVo.getCallNo() + "【异常挂断】");
                amount = amount.add(infoVo.getAmount());
                apiCallUserBillService.updata(infoVo);
            }

            UserVo zbUser = null; //主播
            UserVo cmUser = null;  //会员

            UserVo dialer = apiUserService.queryObject(info.getDialer());//拨号人
            UserVo receiver = apiUserService.queryObject(info.getReceiver());//接听人
            if (userVo.getIdentity() == 2) {
                zbUser = userVo;
                if (userVo.getId() == dialer.getId()) {
                    cmUser = receiver;
                } else {
                    cmUser = dialer;
                }
            } else {
                cmUser = userVo;
                if (userVo.getId() == dialer.getId()) {
                    zbUser = receiver;
                } else {
                    zbUser = dialer;
                }
            }

            //mouldName 1 live
            apiAmountBillService.consume(zbUser.getId(), "live", amount, cmUser);

            ApiOrderVo apiOrderVo = new ApiOrderVo();
            apiOrderVo.setRemark(info.getCallNo() + "【异常挂断】");
            apiOrderVo.setCreateDate(info.getCreateTime());
            apiOrderVo.setStatus(3);
            apiOrderVo.setMouldId(zbUser.getId());
            apiOrderVo.setMouldName("live");
            apiOrderVo.setCmUserId(cmUser.getId());
            apiOrderVo.setZbUserId(zbUser.getId());
            apiOrderVo.setNum(1);
            apiOrderVo.setPrice(zbUser.getLive_price());
            apiOrderVo.setTotal(amount);
            apiOrderVo.setCommentStatus(2);
            apiOrderService.save(apiOrderVo);


            ApiCallBillVo callBillVo = new ApiCallBillVo();
            callBillVo.setCreateTime(info.getCreateTime());
            callBillVo.setTvAnchor(zbUser.getId());
            callBillVo.setConsumer(cmUser.getId());
            callBillVo.setMode(info.getMode());
            callBillVo.setStatus(1L);
            callBillVo.setTalkTime(talkTime);
            callBillVo.setIntimacy(amount);
            callBillVo.setConsumption(amount);
            apiCallBillService.save(callBillVo);

        }


    }
}
